package com.chainyoung.component_tmperson.ui.mvp.presenter;

import com.chainyoung.common.appcommon.entity.HdWalletCache;
import com.chainyoung.common.appcommon.entity.RecieveAccount;
import com.chainyoung.common.base.CommonSubscriber;
import com.chainyoung.common.di.scope.ActivityScope;
import com.chainyoung.common.exception.ApiException;
import com.chainyoung.common.mvp.BasePresenter;
import com.chainyoung.common.utils.RxUtils;
import com.chainyoung.component_tmperson.entity.CoinItem;
import com.chainyoung.component_tmperson.entity.InviteFriend;
import com.chainyoung.component_tmperson.ui.mvp.contract.GetCoinContract;
import com.chainyoung.component_tmperson.ui.mvp.contract.InviteFriendContract;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import javax.inject.Inject;

import static com.chainyoung.common.constant.LibConstant.NO_RESULT_ERRORCODE;

@ActivityScope
public class GetCoinListPreseneter extends BasePresenter<GetCoinContract.Model, GetCoinContract.View> {

    @Inject
    public GetCoinListPreseneter(GetCoinContract.Model model, GetCoinContract.View rootView) {
        super(model, rootView);
    }


    public void getCoinList(TreeMap<String, Object> map) {
        addSubscribe(mModel.getCoinList(map)
                .compose(RxUtils.<List<CoinItem>>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<List<CoinItem>>(mView,true) {
                    @Override
                    public void onNext(List<CoinItem> data) {
                        mView.onGetCoinListSuccess(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e instanceof ApiException) {
                            if (((ApiException) e).getErrorCode().equals(NO_RESULT_ERRORCODE)) {
                                mView.onGetCoinListSuccess(null);
                                mView.hideLoading();
                                return;
                            }
                        }
                        mView.onGetCoinListFailed();
                        super.onError(e);
                    }
                }));
    }

    public void selectCoin(TreeMap<String, Object> map) {
        addSubscribe(mModel.selectCoin(map)
                .compose(RxUtils.<String>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<String>(mView,true) {
                    @Override
                    public void onNext(String data) {
                        mView.onSelectCoinSuccess(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                }));
    }


    public void getAllHdWalletCaches(String coinType) {
        addSubscribe(mModel.getAllHdWalletCaches(coinType)
                .compose(RxUtils.<List<HdWalletCache>>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<List<HdWalletCache>>(mView) {
                    @Override
                    public void onNext(List<HdWalletCache> data) {
                        mView.onGetAllHdWalletCaches(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                }));


    }


    public void addHdWalletCache(HdWalletCache item) {
        addSubscribe(mModel.addHdWalletCache(item)
                .compose(RxUtils.<HdWalletCache>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<HdWalletCache>(mView,true) {
                    @Override
                    public void onNext(HdWalletCache data) {
                        mView.onAddHdWalletCacheSuccess(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                }));
    }


}

